<?php

namespace App\Web\Controller;

use App\Web\Facades\UserAuth;
use App\Web\Model\OperatorLog;
use App\Web\Service\RightCheck;
use EchoPHP\Facades\Db;
use EchoPHP\Http\Request;
use App\Web\Model\Member as MemberModel;


class Member extends Controller {

    public $rightCheck = [
        'index' => RightCheck::RIGHT_ACCOUNT_USER,
        'save' => RightCheck::RIGHT_ACCOUNT_USER,
        'remove' => RightCheck::RIGHT_ACCOUNT_USER,
    ];

    /**
     * 查询团队成员列表
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function index(Request $request) {

        $pageNum = $request->getParam('pagenum/d', 1);
        $pageLimit = $request->getParam('pagelimit/d');
        $oprRole = UserAuth::getLoginInfo('issuper', '');
        if ($oprRole == 1) {
            $teamCode = $request->getParam('teamcode');
        } else {
            $teamCode = substr(UserAuth::getLoginInfo('teamcode', ''), 0, 5);
        }
        $search = [
            'cname' => $request->getParam('cname'),
            'userid' => $request->getParam('userid'),
            'username' => $request->getParam('username'),
            'teamcode' => $request->getParam('teamcode'),
            'withchild' => $request->getParam('withchild', 0),
            'teamrole' => $request->getParam('teamrole'),
            'islock' => $request->getParam('islock'),     //在职状态改锁定状态
            'teammember' => $request->getParam('teammember', 1),//查询团队成员
        ];

        $memberModel = new MemberModel();
        list($pageNum, $rowFrom, $rowTo) = $memberModel->paginationParameters($pageNum, $pageLimit);
        $resultBody = [];
        $count = $memberModel->getMemberListCount($search, $teamCode);
        $resultBody['totalCount'] = intval($count);
        $resultBody['pagenum'] = $pageNum;
        $memberList = $memberModel->getMemberList($search, $rowFrom, $rowTo, $teamCode);

        return $this->json(array_merge($resultBody, $memberList));
    }

    /**
     * 修改团队成员信息
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function save(Request $request) {
        //获取提交参数
        $userId = $request->getPost('userid/d', 0);
        $teamCode = $request->getPost('teamcode');
        $teamRole = $request->getPost('teamrole');
        $leader = $request->getPost('leader');

        //执行
        $memberModel = new MemberModel();
        $executeResult = $memberModel->saveMemberInfo($userId, $teamCode, $teamRole, $leader);
        return $this->json($executeResult);
    }

    /**
     * 删除团队成员，必须指定需要删除的团队成员id
     * @param Request $request
     * @return \EchoPHP\Http\Response\JsonResponse
     * @throws \Exception
     */
    public function remove(Request $request) {
        $oprId = $this->getLoginId();
        $userId = $request->getPost('userid/d');
        $memberModel = new MemberModel();
        $executeResult = $memberModel->removeMember($oprId, $userId);
        return $this->json($executeResult);
    }
}
